<template>
	<!-- 盘点录入 -->
	<view class>
		<view class="mater-list">
			<view class="mater-item font-35" v-for="(item ,index) in checkOrder" :key="index">
				<view class="cu-form-group">
					<view class="title">物资单号</view>
					<view class="text-bold text-blue">{{ item.PK_MATERIAL || '' }}</view>
				</view>
				<view class="cu-form-group">
					<view class="title">名称</view>
					<view>{{ item.VNAME || item.MATERIALNAME }}</view>
				</view>

				<view class="cu-form-group">
					<view class="title">规格</view>
					<view>{{ item.MATSPEC || '' }}</view>
				</view>
				<view class="cu-form-group">
					<view class="title">货位</view>
					<view>{{ item.STATIONNAME || '' }}</view>
				</view>

				<!-- <view class="cu-form-group">
					<view class="title">查看货位</view>
					<button class="cu-btn bg-blue shadow round" @tap="openScan('STATION')">
						<span class="cuIcon-recordfill"></span>
					</button>
				</view> -->

				<view class="cu-form-group">
					<view class="title">库存数量</view>
					<view>{{ item.KNUM || '' }} {{ item.UNIT || '' }}</view>
				</view>
				<view class="cu-form-group">
					<view class="title">盘点数量</view>
					<input type="number" disabled v-model="item.NNUM" class="bottom-border font-35" />
				</view>

				<view class="cu-form-group">
					<view class="title">盈亏数量</view>
					<view>{{ item.PYKNUM }}</view>
				</view>
			</view>
		</view>

		<view class="btn-area padding">
			<button hover-class="e-hover-btn" class="login-btn bg-blue" type="primary" @click="submitForm"
				:loading="isDisabled">提交</button>
		</view>

		<!-- <view class="operation">
			<view class="operation-button border-right" @click="finishSubmit">
				完成盘点
			</view>
		</view> -->

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex";
	import api from "@/common/api.js";
	import utils from "@/common/util.js";
	let _self, activeType;
	let queryJson = {
		"StartTime": "",
		"EndTime": "",
		"CREATOR": '' // _self.userInfo.Account
	}
	export default {
		computed: mapState(["userInfo", "baseUrl"]),
		data() {
			return {
				list: [],
				checkOrder: [],
				isDisabled: false,
				mainOrder: {
					BILLNO: "",
					account: "",
					userId: ""
				}
			};
		},
		onLoad(p) {
			_self = this;
			queryJson = {
				"StartTime": "",
				"EndTime": "",
				"CREATOR": '' // _self.userInfo.Account
			}
			getApp().globalData.queryJsonLS = null
			this.getMater()

		},
		
		onNavigationBarButtonTap(e) {
			this.listenOnce()
			uni.navigateTo({
				url: '/pages/public/query?type=menu1011' 
			})
		},

		methods: {
			// 初始化赋值
			initSetData() {
				_self.mainOrder.userId = _self.userInfo.UserId;
				_self.mainOrder.account = _self.userInfo.Account;
			},
			// 计算盈亏数量
			getPDNun(e, i) {
				let val = e.target.value;
				_self.checkOrder[i].IsInventory = 1;
				_self.checkOrder[i].PYKNUM = val - _self.checkOrder[i].KNUM;
			},
			
			// 初始监听
			listenOnce() {
				uni.$off('reload')
				uni.$once("reload", function(data) {
					queryJson= { ...queryJson , ...data }
					getApp().globalData.queryJsonLS = queryJson
					_self.getMater()
				});
			},

			getMater() {
				let URl = '/api/APPCommon/GetInventoryList';
				let data = {
					queryJson: queryJson
				}

				this.axios({
					url: URl,
					data: data,
					isLoading: 1
				}).then(res => {
					let {
						Result
					} = res;
					this.checkOrder = Result.rows
				})
			},

			submitForm() {
				let saveData = {
					..._self.mainOrder,
					Detail: JSON.stringify(_self.checkOrder)
				};
				if (saveData.BILLNO == 'Y') {
					saveData.VDEF2 = 'Y'
					saveData.BILLNO = ''
				}
				let saveURL = _self.baseUrl + api.apiPublic.checkListSubmit;
				_self.isDisabled = true;
				// console.log(saveURL);
				console.log(saveData);
				this.axios({
					url: saveURL,
					data: saveData,
					isLoading: 1
				}).then(res => {
					let {} = res;
					_self.isDisabled = false;
					utils.tips("提交成功");
					uni.$emit('updateList')
					setTimeout(function() {
						uni.navigateBack({
							delta: 1
						})
					}, 1500);
				}).catch(err => {
					_self.isDisabled = false
				})
			},

			openScan(id, index) {
				// _isActiveStore = true ;
				// console.log(id,index)
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						// console.log(res)
						if (res.scanType != "QR_CODE") {
							return uni.showToast({
								icon: "none",
								title: "类型错误，请重新扫描"
							});
						}
						let t = res.result.replace(/'/g, '"');
						let data = JSON.parse("{" + t + "}");
						let sid = "PK_" + id,
							sname = id + "NAME";
						// console.log(sid);
						// console.log(sname);
						if (id == "STORE") {
							// 仓库
							_self.mainOrder.PK_STORE = data.PK_STORE;
							_self.mainOrder.STORENAME = data.STORENAME;
						} else if (id == "STATION") {
							// 货位
							_self.mainOrder[sname] = data[sname];
							_self.mainOrder[sid] = data[sid];
						} else if (id == "STOREAREA") {
							// 货区
							_self.mainOrder[sname] = data.STATIONNAME;
							_self.mainOrder[sid] = data.PK_STATION;
						} else if (id == "STORESHELVES") {
							//货架
							_self.mainOrder[sname] = data.STATIONNAME;
							_self.mainOrder[sid] = data.PK_STATION;
						} else if (id == "STORELAYER") {
							//货层
							_self.mainOrder[sname] = data.STATIONNAME;
							_self.mainOrder[sid] = data.PK_STATION;
						}
					}
				});
			},

			// 完成盘点提交
			finishSubmit() {
				let saveData = {
					..._self.mainOrder,
					TYPE: 'ST_INVENTORY',
					Detail: JSON.stringify(_self.checkOrder)
				};
				console.log(saveData);
				let saveURL = _self.baseUrl + '/api/APPCommon/FinishBillSubmit';
				this.axios({
					url: saveURL,
					data: saveData,
					isLoading: 1
				}).then(res => {
					let {
						Message,
						Result
					} = res;
					_self.$utils.tips('提交成功')
					setTimeout(r => {
						uni.navigateBack({
							delta: 2
						})
					}, 1600)
				})

			}

		}
	};
</script>

<style scoped>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
		font-size: 35rpx;
	}

	.btn-area {
		margin-bottom: 100rpx;
	}

	.mater-item {
		width: 680rpx;
		padding: 14rpx 10rpx;
		margin: 10rpx auto;
		background-color: #ffffff;
		border-radius: 16rpx;
	}

	.bottom-border {
		border-bottom: 1rpx solid #007aff;
		text-align: right;
	}

	.bottom-border {
		text-align: left;
	}

	.bottom-border:focus-within {
		border-bottom: 1rpx solid #007aff;
	}
</style>